package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api("菜品分类")
@Slf4j
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService service;

    /**
     * query参数
     *     这里的开发文档上面的请求参数是query形式，也就是地址栏＋问号？的形式
     *     所以就不需要用RequestBody
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = service.queryPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> page(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        //根据菜品id查询菜品信息和菜品口味信息，完成菜品信息数据回显
        DishVO dishVO = service.queryDish(id);
        return Result.success(dishVO);
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> getDishCategory(@RequestParam("categoryId") Integer id){
        log.info("根据分类id查询菜品:{}",id);
        List<Dish> dishList = service.getDishByCategoryId(id);
        return Result.success(dishList);
    }
    /**
     * 注意事项
     * 这里要注意的是前端传过来的是一个字符串 常规来说接收是字符串接受  String ids,然后再用，逗号一个一个的分隔开
     *  而这里用的是集合接收了，所以这里要添加一个注解@RequestParm
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}",ids);
        service.deleteBatch(ids);
        //将所有的菜品缓存数据全部删除掉--dish开头
       cleanCache("dish*");
        return Result.success();
    }
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        service.save(dishDTO);

        //清理缓存数据
        String key = "dish:" + dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}",dishDTO);
        service.updateDish(dishDTO);

        //删除所有缓存数据
        Set keys = redisTemplate.keys("dish*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    @ApiOperation("菜品起售/停售")
    @PostMapping("/status/{status}")
    public Result saveOrUpdate(@PathVariable Integer status,Integer id) {
        service.saveOrUpdate(status,id);
        //删除所有缓存数据
        cleanCache("dish*");
        return Result.success();
    }

    /**
     * 统一清除缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
